package database

import (
	"context"
	"luntan/pkg/models"
	"strconv"

	"github.com/jackc/pgx/v5"
)

func SelectUser(name string, password string) (pgx.Rows, error) {
	DB.Close(context.Background())
	Init()
	sqlstr := `select id,name,pwd from t_user where name=$1 and pwd=$2;`
	rows, err := DB.Query(context.Background(), sqlstr, name, password)
	return rows, err
}

func InsertUser(name string, password string) error {
	DB.Close(context.Background())
	Init()
	sqlstr := `insert into t_user(name,pwd) values($1,$2) returning id;`
	row := DB.QueryRow(context.Background(), sqlstr, name, password)
	var userID int64
	err := row.Scan(&userID)
	return err
}

func SelectList() (pgx.Rows, error) {
	DB.Close(context.Background())
	Init()
	sqlstr := `select id,owner,title,description,content from commit_list;`
	rows, err := DB.Query(context.Background(), sqlstr)
	return rows, err
}

func SelectIdList(id string) pgx.Row {
	DB.Close(context.Background())
	Init()
	sqlstr := `select id,owner,title,description,content from commit_list where id=$1;`
	row := DB.QueryRow(context.Background(), sqlstr, id)
	return row
}

func InsertList(owner string, title string, description string, content string) int64 {
	DB.Close(context.Background())
	Init()
	sqlstr := `insert into commit_list(owner,title,description,content) values($1,$2,$3,$4) returning id;`
	row := DB.QueryRow(context.Background(), sqlstr, owner, title, description, content)
	var userID int64
	row.Scan(&userID)
	return userID
}

func SelectArticle(id string) (pgx.Rows, error) {
	DB.Close(context.Background())
	Init()
	sqlstr := `select id,commiter,agree,disagress,time,content,ischild,father from article_` + id + `;`
	// fmt.Println(sqlstr)
	rows, err := DB.Query(context.Background(), sqlstr)
	// fmt.Println(rows)
	return rows, err
}

func CreateTable(listid int64) error {
	DB.Close(context.Background())
	Init()
	tablename := "article_" + strconv.FormatInt(listid, 10)
	sqlstr := `create table ` + tablename + `(id serial primary key,commiter varchar(32),agree int,disagress int ,time varchar(64),content text,ischild bool,father int);`
	_, err := DB.Query(context.Background(), sqlstr)
	DB.Exec(context.Background(), `commit;`)
	return err
}

func CreateCommit(articl_id string, cmt models.Commit) pgx.Row {
	DB.Close(context.Background())
	Init()
	sqlstr := `insert into article_` + articl_id + `(commiter,agree,disagress,time,content,ischild,father) values ($1,0,0,to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS'),$2,false ,0) returning id;`
	row := DB.QueryRow(context.Background(), sqlstr, cmt.Name, cmt.Content)
	return row
}

func CreateReplyCommit(articl_id string, cmt models.Commit, num int64) pgx.Row {
	DB.Close(context.Background())
	Init()
	sqlstr := `insert into article_` + articl_id + `(commiter,agree,disagress,time,content,ischild,father) values ($1,0,0,to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS'),$2,true ,$3) returning id;`
	row := DB.QueryRow(context.Background(), sqlstr, cmt.Name, cmt.Content, cmt.Father)
	return row
}

func SelectUsers() (pgx.Rows, error) {
	DB.Close(context.Background())
	Init()
	sqlstr := `select * from t_user`
	rows, err := DB.Query(context.Background(), sqlstr)
	return rows, err
}
